/*
  Created by 1271107612@qq.com.
  User: DELL
  Date: 2022/4/20
  Time: 10:02
  DESCRIPTION: 
*/
package com.zzy.face.controller;

import com.zzy.face.dto.RLSchoolAddDto;
import com.zzy.face.model.RLSchool;
import com.zzy.face.model.Student;
import com.zzy.face.service.impl.RLSchoolServiceImpl;
import com.zzy.face.service.impl.StudentServiceImpl;
import com.zzy.face.service.impl.TeacherServiceImpl;
import com.zzy.face.utils.Result;
import com.zzy.face.utils.ResultCode;
import com.zzy.face.utils.SnowflakeIdWorker;
import com.zzy.face.utils.jwt.Jwt;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@Api(tags = "返离校")
public class RLSchoolController {
    final RLSchoolServiceImpl rlSchoolService;
    final StudentServiceImpl studentService;
    final TeacherServiceImpl teacherService;
    final Jwt jwt;
    public RLSchoolController(RLSchoolServiceImpl rlSchoolService, StudentServiceImpl studentService, TeacherServiceImpl teacherService, Jwt jwt) {
        this.rlSchoolService = rlSchoolService;
        this.studentService = studentService;
        this.teacherService = teacherService;
        this.jwt = jwt;
    }

    @RequestMapping(value = "/rl", method = RequestMethod.POST)
    @ApiOperation(value = "提交返校离校申请", notes = "提交返校离校申请")
    public Result addRLSchool(@RequestBody RLSchoolAddDto form,HttpServletRequest request) {
        RLSchool rlSchool = new RLSchool();
        if (form.getUid().toString().length()>5){
            rlSchool.setUid(form.getUid());
        }else{
            rlSchool.setUid(SnowflakeIdWorker.getUUID());
        }
        Student student = studentService.findById(Long.valueOf(jwt.token(request).getId()));
        long teacher_id = studentService.findMyTeacher(student.getUid());
        rlSchool.setDate_time(form.getDate_time());
        rlSchool.setTrain_number(form.getTrain_number());
        rlSchool.setPlace(form.getPlace());
        rlSchool.setMethod(form.getMethod());
        rlSchool.setState(0);
        rlSchool.setType(form.getType());
        rlSchool.setTeacher(teacherService.findTeacherById(teacher_id));
        rlSchool.setStudent(student);
        rlSchoolService.save(rlSchool);
        return new Result(ResultCode.SUCCESS);
    }

    @RequestMapping(value = "/rl/{rlid}", method = RequestMethod.PUT)
    @ApiOperation(value = "修改状态", notes = "修改状态")
    public Result updateRLSchool(@RequestBody RLSchoolAddDto form,@PathVariable("rlid") Long rlid,HttpServletRequest request) {
        RLSchool rlSchool = rlSchoolService.findById(rlid);
        int identity =Integer.parseInt( jwt.token(request).get("identity").toString());
        if (identity==0){
            rlSchool.setState(-1);
        }
        else{
            rlSchool.setState(form.getState());
        }
        rlSchoolService.save(rlSchool);
        return new Result(ResultCode.SUCCESS);
    }

    @RequestMapping(value = "/rl/{rlid}", method = RequestMethod.DELETE)
    public Result delRLSchool(@PathVariable("rlid") Long rlid) {
        RLSchool rlSchool = rlSchoolService.findById(rlid);
        rlSchool.setState(-2);
        rlSchoolService.save(rlSchool);
        return new Result(ResultCode.SUCCESS);
    }


    @RequestMapping(value = "/teacher/rl", method = RequestMethod.GET)
    @ApiOperation(value = "获取老师可管理的返离校信息", notes = "")
    public Result getRLByTeacherId(HttpServletRequest request) {
        Long id = Long.valueOf(jwt.token(request).getId());
        return new Result(ResultCode.SUCCESS,rlSchoolService.findByTeacher(id));
    }

    @RequestMapping(value = "/teacher/r", method = RequestMethod.GET)
    @ApiOperation(value = "获取学生本人的返校信息", notes = "")
    public Result getRByTeacherId(HttpServletRequest request) {
        Long id = Long.valueOf(jwt.token(request).getId());
        return new Result(ResultCode.SUCCESS,rlSchoolService.findRByTeacher(id));
    }

    @RequestMapping(value = "/teacher/l", method = RequestMethod.GET)
    @ApiOperation(value = "获取学生本人的离校信息", notes = "")
    public Result getLByTeacherId(HttpServletRequest request) {
        Long id = Long.valueOf(jwt.token(request).getId());
        return new Result(ResultCode.SUCCESS,rlSchoolService.findLByTeacher(id));
    }

    @RequestMapping(value = "/student/rl", method = RequestMethod.GET)
    @ApiOperation(value = "获取学生本人的返离校信息", notes = "")
    public Result getRLByStudentId(HttpServletRequest request) {
        Long id = Long.valueOf(jwt.token(request).getId());
        return new Result(ResultCode.SUCCESS,rlSchoolService.findByStudent(id));
    }

    @RequestMapping(value = "/student/r", method = RequestMethod.GET)
    @ApiOperation(value = "获取学生本人的返校信息", notes = "")
    public Result getRByStudentId(HttpServletRequest request) {
        Long id = Long.valueOf(jwt.token(request).getId());
        return new Result(ResultCode.SUCCESS,rlSchoolService.findRByStudent(id));
    }

    @RequestMapping(value = "/student/l", method = RequestMethod.GET)
    @ApiOperation(value = "获取学生本人的离校信息", notes = "")
    public Result getLByStudentId(HttpServletRequest request) {
        Long id = Long.valueOf(jwt.token(request).getId());
        return new Result(ResultCode.SUCCESS,rlSchoolService.findLByStudent(id));
    }



    //    @RequestMapping(value = "/rl/{rlid}", method = RequestMethod.GET)
//    @ApiOperation(value = "获取返离校信息", notes = "")
//    public Result getOneById(@PathVariable("rlid") Long rlid) {
//        RLSchool rlSchool = rlSchoolService.findById(rlid);
//        return new Result(ResultCode.SUCCESS,rlSchool);
//    }
//    @RequestMapping(value = "/teacher/rl/{rlid}", method = RequestMethod.PUT)
//    @ApiOperation(value = "老师对可管理的返离校信息进行修改", notes = "")
//    public Result updateRLSchool(@PathVariable("rlid") Long rlid,@RequestBody) {
//        return new Result(ResultCode.SUCCESS,rlSchoolService.findByTeacher(id));
//    }
}
